Add -Dinic’s Algorithm#249
Hidden character warning
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR adds a comprehensive implementation of Dinic's Algorithm for computing maximum flow in directed networks. The algorithm uses level graphs (BFS) and blocking flows (DFS) to efficiently find the maximum flow from a source to a sink vertex, offering better performance than classical Ford-Fulkerson approaches.
Key changes:
- Complete Dinic's Algorithm implementation with BFS level graph construction and DFS blocking flow computation
- Support for flow network creation, edge addition, and flow visualization functions
- Four comprehensive examples demonstrating different use cases (basic networks, bipartite matching, multi-source/multi-sink)
|
@siriak please have a look |
| create_flow_network <- function(n) { | ||
| # Use environment to allow in-place (by-reference) mutation without superassignment | ||
| env <- new.env(parent = emptyenv()) | ||
| env$n <- n | ||
| env$graph <- vector("list", n) # Adjacency list | ||
| env$capacity <- matrix(0, nrow = n, ncol = n) # Capacity matrix | ||
| env$flow <- matrix(0, nrow = n, ncol = n) # Flow matrix | ||
| return(env) | ||
| } |
There was a problem hiding this comment.
The function documentation uses an incorrect documentation format. R function documentation should use proper roxygen2 syntax with @param tags, not @param: with colons. The return value should be documented with @return without a colon. Similarly for other functions throughout the file.
| # ========== Example 1: Basic 6-Vertex Network ========== | ||
|
|
||
| cat("========== Example 1: Basic 6-Vertex Flow Network ==========\n\n") |
There was a problem hiding this comment.
The file contains executable example code at the module level (lines 251-398). According to repository guidelines, examples should be included as commented code within the script, not as executable statements that run when the file is sourced. Consider commenting out or removing the executable examples or moving them to a separate example/demo file.
PR: Dinic's Algorithm for Maximum Flow
This PR provides a comprehensive R implementation of Dinic's Algorithm, a highly efficient algorithm to compute the maximum flow in a flow network. The algorithm leverages level graphs and blocking flows to improve performance over classical methods like Ford-Fulkerson.
Overview
Dinic's Algorithm operates in two main phases iteratively:
Level Graph Construction (BFS):
Blocking Flow Computation (DFS):
These steps are repeated until no augmenting path exists from the source to the sink. The approach ensures efficient convergence and allows multiple flows per BFS iteration.
Features
Complexity
Applications
Demonstration
Example 1: Basic 6-Vertex Network